//
//  ShopInfoViewController.m
//  Liuhe
//
//  Created by zr on 2018/8/9.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "ShopInfoViewController.h"

@interface ShopInfoViewController ()

@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UILabel * companyLabel;
@property (nonatomic, strong)UILabel * timeLabel;
@property (nonatomic, strong)UILabel * watchLabel;
@property (nonatomic, strong)UILabel * categoryLabel;
@property (nonatomic, strong)UILabel * sourceLabel;
@property (nonatomic, strong)UILabel * adressLabel;
@property (nonatomic, strong)UILabel * personLabel;
@property (nonatomic, strong)UILabel * telLabel;
@property (nonatomic, strong)UILabel * categoryValueLabel;
@property (nonatomic, strong)UILabel * sourceValueLabel;
@property (nonatomic, strong)UILabel * personValueLabel;
@property (nonatomic, strong)UILabel * adressValueLabel;
@property (nonatomic, weak)UILabel * telValueLabel;
@property (nonatomic, strong)UIView * lineView;
@property (nonatomic, strong)UIView * middleView;
@property (nonatomic, weak)UILabel * infoLabel;
@property (nonatomic, weak)UITextView * textView;

@end

@implementation ShopInfoViewController


- (void)viewWillAppear:(BOOL)animated{
    
    NSDictionary * dict = @{@"id":self.ID};
    
    [[NetworkTool sharedTools] POST:@"/index/Liuheonline/companyDetails" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        int status = [responseObject[@"status"] intValue];
        
        if (status == 200) {
            
            NSDictionary * data = responseObject[@"data"];
            
            self.companyLabel.text = data[@"name"];
            // timeStampString 是服务器返回的13位时间戳
            NSString *timeStampString  = data[@"ctime"];
            
            // iOS 生成的时间戳是10位
            NSTimeInterval interval    =[timeStampString doubleValue];
            NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
            
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSString *dateString       = [formatter stringFromDate: date];
            self.timeLabel.text = [NSString stringWithFormat:@"更新：%@",dateString];
            NSString * watchStr = [NSString stringWithFormat:@"%@",data[@"visit"]];
            self.watchLabel.text = [NSString stringWithFormat:@"浏览：%@",watchStr];
            self.categoryValueLabel.text = data[@"category"];
            self.sourceValueLabel.text = data[@"source"];
            self.personValueLabel.text = data[@"contacts"];
            self.adressValueLabel.text = data[@"address"];
            self.telValueLabel.text = data[@"mobile"];
            self.textView.text = data[@"content"];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = WHITE_COLOR;
    
    [self setupNav];
    [self setupUI];
    
}


- (void)setupNav{
    
    self.title = @"详情";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}

- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)setupUI{
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.text = @"公司名称:";
    titleLabel.font = [UIFont systemFontOfSize:16 * SCREENH6];
    titleLabel.textColor = ColorWithRGB(0x999999);
    
    self.titleLabel = titleLabel;
    
    [self.view addSubview:self.titleLabel];
    
    
    UILabel * companyLabel = [UILabel new];
    
    companyLabel.text = @"正飞网络科技有限公司";
    companyLabel.font = [UIFont systemFontOfSize:16 * SCREENH6];
    companyLabel.textColor = BLACK_COLOR;
    
    self.companyLabel = companyLabel;
    
    [self.view addSubview:self.companyLabel];
    
    
    self.timeLabel = [self setupLabel:@"更新：2018-07-29" andType:@"normal"];
    [self.view addSubview:self.timeLabel];
    
    self.watchLabel = [self setupLabel:@"浏览：362485人" andType:@"normal"];
    [self.view addSubview:self.watchLabel];
    
    self.categoryLabel = [self setupLabel:@"类别：" andType:@"normal"];
    [self.view addSubview:self.categoryLabel];
    
    self.sourceLabel = [self setupLabel:@"来源：" andType:@"normal"];
    [self.view addSubview:self.sourceLabel];
    
    self.adressLabel = [self setupLabel:@"详细地址：" andType:@"normal"];
    [self.view addSubview:self.adressLabel];
    
    self.personLabel = [self setupLabel:@"联系人：" andType:@"normal"];
    [self.view addSubview:self.personLabel];
    
    self.telLabel = [self setupLabel:@"联系电话：" andType:@"normal"];
    [self.view addSubview:self.telLabel];
    
    self.categoryValueLabel = [self setupLabel:@"网站建设/推广" andType:@"black"];
    [self.view addSubview:self.categoryValueLabel];
    
    self.sourceValueLabel = [self setupLabel:@"公司" andType:@"black"];
    [self.view addSubview:self.sourceValueLabel];
    
    self.adressValueLabel = [self setupLabel:@"东路1886号" andType:@"black"];
//    self.adressValueLabel.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:self.adressValueLabel];
    
    self.personValueLabel = [self setupLabel:@"正飞系统" andType:@"black"];
    [self.view addSubview:self.personValueLabel];

    
    UILabel * telValueLabel = [UILabel new];
    
    telValueLabel.text = @"13582492533";
    telValueLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    telValueLabel.textColor = [UIColor redColor];
    
    self.telValueLabel = telValueLabel;
    
    [self.view addSubview:self.telValueLabel];
    
    self.lineView = [self setupLineView];
    [self.view addSubview:self.lineView];
    
    self.middleView = [self setupLineView];
    [self.view addSubview:self.middleView];
    
    
    UILabel * infoLabel = [UILabel new];
    
    infoLabel.text = @"服务详情";
    infoLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    infoLabel.textColor = MAINCOLOR;
    
    self.infoLabel = infoLabel;
    
    [self.view addSubview:self.infoLabel];
    
    
    UITextView *textView =[[UITextView alloc] init];
    textView.backgroundColor= [UIColor whiteColor];
//    textView.text = data[@"content"];
    textView.textColor = [UIColor blackColor];
    textView.editable = NO;
    textView.font = [UIFont systemFontOfSize:13 * SCREENH6];
    
    self.textView = textView;
    
    [self.view addSubview:self.textView];
}


-(void)viewDidLayoutSubviews{
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(10 * SCREENH6);
    }];
    
    [self.companyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.titleLabel);
        make.leading.mas_equalTo(self.titleLabel.mas_trailing).offset(8 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.titleLabel);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(7 * SCREENH6);
    }];
    
    [self.watchLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.timeLabel);
        make.leading.mas_equalTo(self.timeLabel.mas_trailing).offset(50 * SCREENH6);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.timeLabel.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.categoryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.timeLabel);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.categoryValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.categoryLabel);
        make.leading.mas_equalTo(self.categoryLabel.mas_trailing);
    }];
    
    [self.sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(210 * SCREENH6);
        make.centerY.mas_equalTo(self.categoryValueLabel);
    }];
    
    [self.sourceValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.sourceLabel.mas_trailing);
        make.centerY.mas_equalTo(self.sourceLabel);
    }];
    
    [self.personLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.categoryLabel);
        make.top.mas_equalTo(self.categoryLabel.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.personValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.personLabel);
        make.leading.mas_equalTo(self.personLabel.mas_trailing);
    }];
    
    [self.telLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.sourceLabel);
        make.centerY.mas_equalTo(self.personValueLabel);
    }];
    
    [self.telValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.telLabel.mas_trailing);
        make.centerY.mas_equalTo(self.telLabel);
    }];

    [self.adressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.personLabel);
        make.top.mas_equalTo(self.personLabel.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.adressValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.adressLabel.mas_trailing);
        make.centerY.mas_equalTo(self.adressLabel);
    }];
    
    [self.middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(2 * SCREENH6);
        make.top.mas_equalTo(self.adressLabel.mas_bottom).offset(12 * SCREENH6);
    }];
    
    [self.infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.personLabel);
        make.top.mas_equalTo(self.middleView.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.bottom.mas_equalTo(self.view).offset(-10 * SCREENH6);
        make.top.mas_equalTo(self.infoLabel.mas_bottom).offset(10 * SCREENH6);
    }];

}


- (UILabel *)setupLabel:(NSString *)string andType:(NSString *)type{
    
    UILabel * label = [UILabel new];
    
    if ([type isEqualToString:@"black"]) {
        
        label.textColor = BLACK_COLOR;
    }else if ([type isEqualToString:@"normal"]){
        
        label.textColor = ColorWithRGB(0x999999);
    }
    
    label.font = [UIFont systemFontOfSize:13 * SCREENH6];
    label.text = string;
    
    return label;
}


- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xe6e6e6);
    
    return lineView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
